home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-09-17 | 14.7 KB | 478 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: SLGC.cpp
- // Release Version: $ ODF 2 $
- //
- // Copyright: (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "FWOS.hpp"
-
- #ifndef SLGC_H
- #include "SLGC.h"
- #endif
-
- #ifndef SLMAPING_H
- #include "SLMaping.h"
- #endif
-
- #ifndef PRGRDEV_H
- #include "PRGDev.h"
- #endif
-
- #ifndef FWRECT_H
- #include "FWRect.h"
- #endif
-
- #ifndef FWODGEOM_H
- #include "FWODGeom.h"
- #endif
-
- #ifndef SLGRGLOB_H
- #include "SLGrGlob.h"
- #endif
-
- #ifndef SOM_ODShape_xh
- #include <Shape.xh>
- #endif
-
- #ifdef FW_BUILD_MAC
- #pragma segment FW_GraphicsContext
- #endif
-
- //========================================================================================
- // Local helper prototypes
- //========================================================================================
-
- static void PrivSuspend(FW_SGraphicContext* gc);
- static void PrivResume(FW_SGraphicContext* gc);
-
- //========================================================================================
- // struct FW_SGraphicContext
- //========================================================================================
-
- // The one and only current GC
-
- static FW_SGraphicContext* FW_gLastGC = NULL;
-
- //----------------------------------------------------------------------------------------
- // FW_PrivGC_GetCurrent
- //----------------------------------------------------------------------------------------
-
- FW_SGraphicContext* SL_API FW_PrivGC_GetCurrent(Environment* ev)
- {
- FW_UNUSED(ev);
- // No try block necessary - Do not throw
- return FW_gLastGC;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PrivGC_Clear
- //----------------------------------------------------------------------------------------
-
- void SL_API FW_PrivGC_Clear(Environment* ev, FW_SGraphicContext& gc)
- {
- // No try block necessary - Do not throw
- gc.fInitialized = FALSE;
- gc.fPreviousGraphicContext = FW_gLastGC;
- gc.fDevicePreviousState = NULL;
- gc.fDeviceSuspendState = NULL;
- gc.fEnvironment = ev;
- gc.fTransform = NULL;
-
- FW_PrivMapping_Init(gc.fMapping, FW_kPoint);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PrivGC_Initialize
- //
- // Exception Handling Notes
- // Don't forget kids, if we fail during construction then our destructor *won't*
- // get called - i.e. CloseDevice won't be called to match the OpenDevice, etc.
- // We need to protect ourselves from things like SetClip failing (and yes, even stupid
- // little clip operations *do* fail).
- // We could use helper objects, but it seems easier to use nested try/catch blocks in
- // this case since we'd have no opportunity to reuse the helper classes.
- //----------------------------------------------------------------------------------------
-
- void SL_API FW_PrivGC_Initialize(Environment* ev,
- FW_SGraphicContext& gc,
- FW_HGDevice graphicDevice,
- ODTransform* transform,
- ODShape* clipShape)
- {
- FW_SOM_TRY
- {
- FW_ASSERT(graphicDevice != NULL);
-
- if (gc.fPreviousGraphicContext)
- PrivSuspend(gc.fPreviousGraphicContext);
-
- short level;
- FW_VOLATILE(level);
-
- FW_TRY
- {
- // ----- Save the transform ----
- level = 0;
- gc.fTransform = transform;
- if(gc.fTransform != NULL)
- {
- #ifdef FW_BUILD_WIN
- gc.fTransform->IncrementRefCount(ev);
- #endif
- #ifdef FW_BUILD_MAC
- gc.fTransform->Acquire(ev);
- #endif
- }
-
- // ----- Set the graphic device -----
- level = 1;
- gc.fGraphicDevice = graphicDevice;
- gc.fGraphicDevice->Acquire();
-
- // ----- Open the Device -----
- level = 2;
- gc.fDevicePreviousState = gc.fGraphicDevice->OpenDevice(ev, &gc);
-
- // ----- Set the clip -----
- level = 3;
- FW_CHECK_THROW_POINT (FW_CGraphicContext_InitGraphicContext);
- FW_PrivGC_SetClip(ev, gc, clipShape);
- FW_FailOnEvError(ev);
- }
- FW_CATCH_BEGIN
- FW_CATCH_EVERYTHING ()
- {
- if (level >= 3)
- gc.fGraphicDevice->CloseDevice(ev, gc.fDevicePreviousState);
-
- if (level >= 2)
- gc.fGraphicDevice->Release();
-
- if (level >= 1)
- {
- if (gc.fTransform != NULL)
- gc.fTransform->Release(ev);
- }
-
- if (gc.fPreviousGraphicContext)
- PrivResume(gc.fPreviousGraphicContext);
- FW_THROW_SAME ();
- }
- FW_CATCH_END
-
- FW_gLastGC = &gc;
- gc.fInitialized = TRUE;
- }
- FW_SOM_CATCH
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PrivGC_ChangeClipAndTransform
- //----------------------------------------------------------------------------------------
-
- void SL_API FW_PrivGC_ChangeClipAndTransform(Environment* ev,
- FW_SGraphicContext& gc,
- ODTransform* transform,
- ODShape* clipShape)
- {
- FW_SOM_TRY
- {
- if (gc.fTransform != NULL)
- gc.fTransform->Release(ev);
-
- // ----- Save the transform ----
- gc.fTransform = transform;
- if(gc.fTransform != NULL)
- {
- #ifdef FW_BUILD_WIN
- gc.fTransform->IncrementRefCount(ev);
- #endif
- #ifdef FW_BUILD_MAC
- gc.fTransform->Acquire(ev);
- #endif
- }
-
- // ----- Set the origin offset -----
- gc.fGraphicDevice->UpdateOriginForContext(ev);
-
- // ----- Set the clip -----
- FW_PrivGC_SetClip(ev, gc, clipShape);
- FW_FailOnEvError(ev);
- }
- FW_SOM_CATCH
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PrivGC_Terminate
- //----------------------------------------------------------------------------------------
-
- void SL_API FW_PrivGC_Terminate(Environment* ev, FW_SGraphicContext& gc)
- {
- FW_SOM_TRY
- {
- if(gc.fInitialized)
- {
- // ----- Close the Device -----
- if (gc.fDevicePreviousState)
- gc.fGraphicDevice->CloseDevice(ev, gc.fDevicePreviousState);
-
- // ----- Now we can release the device -----
- gc.fGraphicDevice->Release();
- gc.fGraphicDevice = NULL;
-
- // ----- and the transform -----
- if(gc.fTransform != NULL)
- gc.fTransform->Release(ev);
-
- // ----- Reset the last Graphic Context Global -----
- FW_gLastGC = gc.fPreviousGraphicContext;
-
- if (gc.fPreviousGraphicContext)
- PrivResume(gc.fPreviousGraphicContext);
-
- gc.fInitialized = FALSE;
- }
-
- FW_PrivMapping_Term(gc.fMapping, ev);
- }
- FW_SOM_CATCH
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PrivGC_GetClip
- //----------------------------------------------------------------------------------------
-
- ODShape* SL_API FW_PrivGC_GetClip(Environment* ev, const FW_SGraphicContext& gc)
- {
- FW_SOM_TRY
- {
- ODRgnHandle clipRegion = gc.fGraphicDevice->GetClip();
- ODShape* clipShape = ::FW_NewODShape(ev, clipRegion);
- ODShape* clipShapeCopy = FW_PrivGC_DeviceToLogicalShape(ev, gc, clipShape);
- FW_FailOnEvError(ev);
- clipShape->Release(ev); // Disposes the region, too
- return clipShapeCopy;
- }
- FW_SOM_CATCH
-
- return NULL;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PrivGC_SetClip
- //----------------------------------------------------------------------------------------
-
- void SL_API FW_PrivGC_SetClip(Environment* ev, FW_SGraphicContext& gc, ODShape* clipShape)
- {
- FW_ASSERT(clipShape != NULL);
-
- FW_SOM_TRY
- {
- ODShape* clipShapeCopy = FW_PrivGC_LogicalToDeviceShape(ev, gc, clipShape);
- FW_FailOnEvError(ev);
- ODRgnHandle clipRegion = ::FW_GetShapeRegion(ev, clipShapeCopy);
- gc.fGraphicDevice->SetClip(ev, clipRegion); // Uses a copy
- clipShapeCopy->Release(ev); // Disposes the region, too
- }
- FW_SOM_CATCH
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PrivGC_GetClipRect
- //----------------------------------------------------------------------------------------
-
- void SL_API FW_PrivGC_GetClipRect(Environment* ev, const FW_SGraphicContext& gc, FW_SRect& clipRect)
- {
- FW_SOM_TRY
- {
- FW_CPlatformRect plfmClipRect;
- gc.fGraphicDevice->GetClipRect(plfmClipRect);
- FW_PrivGC_DeviceToLogicalRect(ev, gc, plfmClipRect, clipRect);
- FW_FailOnEvError(ev);
- }
- FW_SOM_CATCH
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PrivGC_SetClipRect
- //----------------------------------------------------------------------------------------
-
- void SL_API FW_PrivGC_SetClipRect(Environment* ev, FW_SGraphicContext& gc, const FW_SRect& clipRect)
- {
- FW_SOM_TRY
- {
- FW_CPlatformRect plfmClipRect;
- FW_PrivGC_LogicalToDeviceRect(ev, gc, clipRect, plfmClipRect);
- FW_FailOnEvError(ev);
- gc.fGraphicDevice->SetClipRect(plfmClipRect);
- }
- FW_SOM_CATCH
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PrivGC_SetMapping
- //----------------------------------------------------------------------------------------
-
- void SL_API FW_PrivGC_SetMapping(Environment* ev, FW_SGraphicContext& gc, const FW_SMapping& newMapping)
- {
- FW_SOM_TRY
- {
- FW_PrivMapping_Term(gc.fMapping, ev);
- FW_PrivMapping_InitFromCopy(gc.fMapping, newMapping);
- gc.fGraphicDevice->MappingChanged(ev);
- }
- FW_SOM_CATCH
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PrivGC_GetOriginOffset
- //----------------------------------------------------------------------------------------
-
- void SL_API FW_PrivGC_GetOriginOffset(Environment* ev, const FW_SGraphicContext& gc, FW_PlatformPoint& offset)
- {
- // No try block necessary - Do not throw
- FW_PrivMapping_GetOriginOffset(gc.fMapping, ev, offset, gc.fGraphicDevice, gc.fTransform);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PrivGC_LogicalToDeviceSize
- //----------------------------------------------------------------------------------------
-
- void SL_API FW_PrivGC_LogicalToDeviceSize(Environment* ev,
- const FW_SGraphicContext& gc,
- FW_Fixed xSize,
- FW_Fixed ySize,
- FW_PlatformPoint& ptResult)
- {
- // No try block necessary - Do not throw
- FW_CRect rect(FW_IntToFixed(0), FW_IntToFixed(0), xSize, ySize);
- FW_CPlatformRect plfmRect;
-
- FW_PrivGC_LogicalToDeviceRect(ev, gc, rect, plfmRect);
- // even if error let it go
-
- FW_SetXY(ptResult, plfmRect.right - plfmRect.left, plfmRect.bottom - plfmRect.top);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PrivGC_LogicalToDevicePoint
- //----------------------------------------------------------------------------------------
-
- void SL_API FW_PrivGC_LogicalToDevicePoint(Environment* ev,
- const FW_SGraphicContext& gc,
- const FW_SPoint& point,
- FW_PlatformPoint& ptResult)
- {
- // No try block necessary - Do not throw
- FW_PrivMapping_LogicalToDevicePoint(gc.fMapping, ev, point, ptResult, gc.fGraphicDevice, gc.fTransform);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PrivGC_LogicalToDeviceRect
- //----------------------------------------------------------------------------------------
-
- void SL_API FW_PrivGC_LogicalToDeviceRect(Environment* ev,
- const FW_SGraphicContext& gc,
- const FW_SRect& rect,
- FW_PlatformRect& rectResult)
- {
- // No try block necessary - Do not throw
- FW_PrivMapping_LogicalToDeviceRect(gc.fMapping, ev, rect, rectResult, gc.fGraphicDevice, gc.fTransform);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PrivGC_LogicalToDeviceShape
- //----------------------------------------------------------------------------------------
-
- ODShape* SL_API FW_PrivGC_LogicalToDeviceShape(Environment* ev,
- const FW_SGraphicContext& gc,
- ODShape* shape)
- {
- // No try block necessary - Do not throw
- return FW_PrivMapping_LogicalToDeviceShape(gc.fMapping, ev, shape, gc.fGraphicDevice, gc.fTransform);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PrivGC_DeviceToLogicalSize
- //----------------------------------------------------------------------------------------
-
- void SL_API FW_PrivGC_DeviceToLogicalSize(Environment* ev,
- const FW_SGraphicContext& gc,
- FW_PlatformCoordinate xSize,
- FW_PlatformCoordinate ySize,
- FW_SPoint& ptResult)
- {
- // No try block necessary - Do not throw
- FW_CPlatformRect plfmRect(0, 0, xSize, ySize);
- FW_SRect rect;
- FW_PrivGC_DeviceToLogicalRect(ev, gc, plfmRect, rect);
- // even if error let it go
- ptResult.x = rect.right - rect.left;
- ptResult.y = rect.bottom - rect.top;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PrivGC_DeviceToLogicalPoint
- //----------------------------------------------------------------------------------------
-
- void SL_API FW_PrivGC_DeviceToLogicalPoint(Environment* ev,
- const FW_SGraphicContext& gc,
- const FW_PlatformPoint& point,
- FW_SPoint& ptResult)
- {
- // No try block necessary - Do not throw
- FW_PrivMapping_DeviceToLogicalPoint(gc.fMapping, ev, point, ptResult, gc.fGraphicDevice, gc.fTransform);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PrivGC_DeviceToLogicalRect
- //----------------------------------------------------------------------------------------
-
- void SL_API FW_PrivGC_DeviceToLogicalRect(Environment* ev,
- const FW_SGraphicContext& gc,
- const FW_PlatformRect& rect,
- FW_SRect& rectResult)
- {
- // No try block necessary - Do not throw
- FW_PrivMapping_DeviceToLogicalRect(gc.fMapping, ev, rect, rectResult, gc.fGraphicDevice, gc.fTransform);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PrivGC_DeviceToLogicalShape
- //----------------------------------------------------------------------------------------
-
- ODShape* SL_API FW_PrivGC_DeviceToLogicalShape(Environment* ev, const FW_SGraphicContext& gc, ODShape* shape)
- {
- // No try block necessary - Do not throw
- return FW_PrivMapping_DeviceToLogicalShape(gc.fMapping, ev, shape, gc.fGraphicDevice, gc.fTransform);
- }
-
- //========================================================================================
- // Local helpers
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // PrivSuspend
- //----------------------------------------------------------------------------------------
-
- static void PrivSuspend(FW_SGraphicContext* gc)
- {
- FW_ASSERT(gc->fDeviceSuspendState == NULL);
- gc->fDeviceSuspendState = gc->fGraphicDevice->Suspend();
- }
-
- //----------------------------------------------------------------------------------------
- // PrivResume
- //----------------------------------------------------------------------------------------
-
- static void PrivResume(FW_SGraphicContext* gc)
- {
- gc->fGraphicDevice->Resume(gc->fDeviceSuspendState);
- gc->fDeviceSuspendState = NULL;
- }
-
-
-